/**
 * Environment.java
 * uAPI Public License
 */
package uapi;

import java.util.ArrayList;
import java.util.Map;
import java.util.Iterator;

/**
 * Informations over runtime and operating system
 *
 * @author Bastie - Sebastian Ritter
 * @version 1.0
 * @change returns System.getProperties and System.getenv names and values
 */
public class Environment {

  public Environment () {}

  /**
   * Return the value of an environment name or an empty string if environment
   * not exist.
   * @param name String
   * @return String
   */
  public String getValue (final String name) {
    return (System.getenv(name) == null)
         ? (System.getProperty(name) == null)
           ? "" : System.getProperty(name)
         : System.getenv(name);
  }

  /**
   * Return all environment names
   * @return String []
   */
  public String [] getNames () {
    final ArrayList names = new ArrayList ();
    for (Object key : System.getProperties().keySet()) {
      names.add((String)key);
    }
    Map env = System.getenv();
    for (final Iterator it = env.entrySet().iterator(); it.hasNext(); ) {
       Map.Entry entry = (Map.Entry)it.next();
       names.add((String)entry.getKey());
    }
    String [] retValue = new String [names.size()];
    names.toArray(retValue);
    return retValue;
  }

  /* Destruct */
  protected void finalize () throws Throwable {
  }

}
